limsOrderTask.sampleTaskSearchPage = {

    getBatchProInfoGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'order-edit-list-grid',
            checkOnActive: true,
            page: true,
            filterOpen: true,
            //url: limsOrderTask.batchSampleUrl + '/select/samples',
            url: limsOrderTask.batchSampleUrl + '/select/batchSampleInfos',
            requestData: {},
            columns: limsOrderTask.getSampleListColumns(),
            onLoadSuccess: function (rows) {
                console.log(rows)
                if (Gikam.isNotEmpty(rows)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.loadBatchProList();
                }
            },
            onRowActive: function (index, row) {
                _this.loadBatchProList(row);
            },
            toolbar: [{
                type: 'button',
                text: 'T_LIMS_SAMPLE.SAMPLECODE',
                icon: 'select',
                onClick: function () {
                    var mainGrid = Gikam.getComp('order-edit-list-grid')
                    var model = Gikam.create('modal', {
                        title: 'T_CORE_CHART.SEARCHRULE',
                        height: '300',
                        width: '400',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            Gikam.postText(limsOrderTask.batchSampleUrl + '/select/samples', Gikam.getJsonWrapper({
                                f: {
                                    sampleCode_IN: data.sampleCode.replace(/[(\r\n)|(\ +)|(，)|(；)|(;)]/g, ',')
                                },
                                n : -1
                            })).done(function (data) {
                                console.log('data')
                                data = JSON.parse(data)
                                mainGrid.loadData(data.rows)
                            })
                        },
                    })
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'insert-fileboxnumber-form',
                                fields: [{
                                    field: 'sampleCode',
                                    title: 'T_LIMS_SAMPLE.SAMPLECODE',
                                    type: 'textarea',
                                    colspan: 2,
                                    height: 180,
                                }],
                                onLoadSuccess: function () {
                                },
                                columns: 1,
                                titleWidth: 120
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    icon: 'refresh',
                                    onClick: function () {
                                        var form = Gikam.getComp('insert-fileboxnumber-form')
                                        Gikam.getLastModal().close(form.getData())
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'cancel',
                                    onClick: function () {
                                        Gikam.getLastModal().close()
                                    }
                                }]
                            }]
                        }
                    })
                }
            }],
            onCellClick: function (field, row) {
                if (field === 'ext$.batchcode' && row.ext$.batchid) {
                    Gikam.loadBatchDetailPageV12(row.ext$.batchid, true)
                } else if (field === 'sampleCode' && row.id) {
                    _this.loadSampleTestTaskDetailPage(row)
                }
            },
        }
    },

    loadSampleTestTaskDetailPage: function (row) {
        workspace.window.load(limsOrderTask.baseUrl + '/page/sample-test-task-detail' + Gikam.param({
            batchSampleId: row.id,
        }))
    },

    loadBatchProList: function (row) {
        //模板工序
        Gikam.getAsyncComp('template-list-grid').done(function (grid) {
            grid.refresh({
                url: limsOrderTask.preOrderTaskUrl + '/sample-task/search',
                requestData: {
                    batchSampleId_EQ : row.ext$.batchsampleid
                }
            })
        });
    },

    getBatchTabParam: function () {
        var _this = this;
        return {
            type: 'tab',
            fill: true,
            panels: [{
                title: 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST_METHOD.SERVICE_NAME',
                items: [{
                    type: 'layout',
                    north: {
                        height: '40%',
                        items: [_this.getPreTaskInfoGrid()]
                    },
                    center: {
                        title: 'T_LIMS_ORDER_TASK.SERVICE_NAME',
                        items: [_this.getTaskInfoGrid()]
                    },
                    south: {
                        height: '35%',
                        items: [_this.getAnalyteDerivativeTab()]
                    }
                }]
            }]
        }
    },

    getPreTaskInfoGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'template-list-grid',
            checkOnActive: true,
            columns: limsOrderTask.getTemplateInfoColumns(),
            group: {
                // groupCheckbox : true,
                fields: ['folderId', 'ext$.etname'],
                formatter: function (row) {
                    return row.folderId + "：" + row.ext$.etname;
                }
            },
            order: {
                'folderId': 'asc',
                'ext$.etname': 'asc',
                'sort': 'asc'
            },
            // 选中行
            onRowActive: function (index, row) {
                _this.loadPreTaskList(row);
            },
            onLoadSuccess: function (rows) {
                if (Gikam.isNotEmpty(rows)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.loadPreTaskList();
                }
            },
            toolbar: [],
        }

    },



    loadPreTaskList: function (row) {
        var _this = this;
        Gikam.getAsyncComp('task-info-list-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: limsOrderTask.baseUrl + '/queries',
                    requestData: {
                        preOrderTaskId_EQ: row.id,
                    }
                })
            } else {
                grid.cleanData();
            }
        });
    },


    // 任务信息
    getTaskInfoGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'task-info-list-grid',
            checkOnActive: true,
            service: 'limsOrderTaskServiceImpl',
            columns: limsOrderTask.getTaskInfoColumns(),
            onRowActive: function (index, row) {
                _this.reloadResult(row);
            },
            onLoadSuccess: function (rows) {
                if (Gikam.isNotEmpty(rows)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.reloadResult();
                }
            },
            toolbar: [],
        }
    },
    reloadResult: function (row) {
        var _this = this;
        Gikam.getAsyncComp('tj-order-task-result-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: limsResult.baseUrl + '/queries/raw',
                    requestData: {
                        orderTaskId_EQ: row.id,
                    }
                })
            } else {
                grid.cleanData();
            }
        });

        Gikam.getAsyncComp('order-task-derivative-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: Gikam.printf(limsResult.derivativelUrl + '/tasks/{taskId}/queries', {
                        taskId: row.id
                    }),
                })
            } else {
                grid.cleanData();
            }
        });

        if (row) {
            Gikam.getAsyncComp('result-uploader-task-search').done(function (uploader) {
                uploader.setOptions({
                    bizId: row.runId
                });
            });
        } else {
            Gikam.getAsyncComp('result-uploader-task-search').done(function (uploader) {
                uploader.setOptions({
                    bizId: -1
                });
            });
        }

        Gikam.getAsyncComp('batch-sample-record-list-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: limsResult.auditRecordUrl + '/queries',
                    requestData: {
                        businessId_SEQ: row.id,
                        businessCode_SEQ: 'T_LIMS_ORDER_TASK'
                    }
                });
            } else {
                grid.cleanData();
            }
        });
    },

    getAnalyteDerivativeTab: function () {
        var _this = this;
        return {
            type: 'tab',
            id: 'result-order-task-result-tab',
            panels: [{
                title: 'T_CORE_ITEM.SERVICE_NAME',
                id: 'result-order-task-result-panel',
                items: [{
                    type: 'grid',
                    id: 'tj-order-task-result-grid',
                    page: false,
                    order : {
                        sorter : 'asc'
                    },
                    service: 'resultServiceImpl',
                    columns: limsResult.getResultListColumns(),
                }]
            }, {
                title: 'MODULE.BUSINESS.RESULT.TAB.DERIVATIVE',
                id: 'result-order-task-derivative-panel',
                items: [{
                    type: 'grid',
                    id: 'order-task-derivative-grid',
                    page: false,
                    service: 'sampleServiceImpl',
                    columns: limsResult.getAllDerivativeEditListColumns(),
                }]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                id: 'result-order-task-history-panel',
                items: [{
                    type: 'grid',
                    id: 'batch-sample-record-list-grid',
                    columns: limsResult.getFlowListColumns(),
                }]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'result-uploader-task-search',
                    filter: false,
                    dbTable: 'T_LIMS_RUN',
                    bizId: -1,
                    readonly: true
                }]
            }]
        }
    },

    urlManager: {
        orderTaskGrid: IFM_CONTEXT + '/secure/base-module/business/order-tasks',
        runGrid: limsOrderTask.runUrl,
        resultGrid: limsOrderTask.baseUrl,
        derivativeGrid: IFM_CONTEXT + '/secure/base-module/common/samples'
    },


    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'result-edit-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [_this.getBatchProInfoGrid()]
            }
            /*            west: {
                            width: '30%',
                            title: 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                            items: [_this.getBatchProInfoGrid()]
                        },
                        center: {
                            items: [{
                                type: 'layout',
                                center: {
                                    height: '30%',
                                    items: [_this.getBatchTabParam()]
                                },
                            }]
                        }*/
        });
    },

    init: function (param) {
        this.param = param;

        this.create();
    }
};
